package controller

import (
	"chatgpt/controller/father"
	"chatgpt/helper"
	"chatgpt/helper/code"
	"chatgpt/openai"
	"github.com/gin-gonic/gin"
	"strings"
)

func QuestionHandle(ctx *gin.Context) {
	question := ctx.Query("q")
	mod := ctx.Query("model")
	size := ctx.Query("size")
	//mod = "" // 屏蔽 图片生成
	mcode, replay := openai.RemoteAiGet(question, mod, size)
	helper.Echo(ctx, int(mcode), replay)
}

func ImageGenerateHandle(ctx *gin.Context) {
	question := ctx.Query("q")
	size := ctx.Query("size")

	res := openai.AppChatGPT.ConvImage(question, size)

	helper.Echo(ctx, int(res.MsgCode), res.Text)
}

func OriginHandle(ctx *gin.Context) {
	//question := ctx.Query("q")
	mod := ctx.Query("model")
	switch strings.Trim(mod, " ") {
	case "image-generate":
		ImageGenerateHandle(ctx)
		return
	case "chat":
		ChatHandle(ctx)
		return
	case "conversation":
		ConversationHandle(ctx)
		return
	case "completion", "":
		CompletionHandle(ctx)
		return
	}

	helper.Echo(ctx, int(100), "")
}

func CompletionHandle(ctx *gin.Context) {
	question := father.RawString(ctx, "q")     //ctx.Query("q")
	mod := father.RawString(ctx, "smodel")     //ctx.Query("smodel")
	unsafe := father.RawString(ctx, "unsafe")  //ctx.Query("unsafe")
	stop := father.RawString(ctx, "stops")     //ctx.Query("stops")
	stream_any := father.RawAny(ctx, "stream") //ctx.Query("stops")

	if !openai.AppChatGPT.CheckQuestion(question) && !openai.AppChatGPT.Unsafe(question, unsafe) {
		helper.Echo(ctx, int(code.SUCCESS), "抱歉没有找到与“"+question+"”相关的信息")
		return
	}

	if !openai.StreamCheck(stream_any) {
		res := openai.AppChatGPT.Conv(question, mod, stop)
		//if mcode == code.SUCCESS && !openai.AppChatGPT.Unsafe(question, mod) {
		//	replay = dirtywords.DirtyWords().Replace(replay)
		//}
		res.Echo(ctx)
		return
	} else {
		openai.AppChatGPT.ConvCompletionStream(ctx.Writer, question, mod, stop)
	}

}

func ChatHandle(ctx *gin.Context) {
	question := father.RawString(ctx, "q")    //ctx.Query("q")
	mod := father.RawString(ctx, "smodel")    //ctx.Query("smodel")
	unsafe := father.RawString(ctx, "unsafe") //ctx.Query("unsafe")
	stop := father.RawString(ctx, "stops")    //ctx.Query("stops")
	uid := father.RawString(ctx, "uid")       //ctx.Query("uid")

	if !openai.AppChatGPT.CheckQuestion(question) && !openai.AppChatGPT.Unsafe(question, unsafe) {
		helper.Echo(ctx, int(code.SUCCESS), "抱歉没有找到与“"+question+"”相关的信息")
		return
	}

	if !openai.StreamCheck(father.RawAny(ctx, "stream")) {
		res := openai.AppChatGPT.ConvChat(question, mod, &openai.User{UID: uid}, stop)
		//if mcode == code.SUCCESS && !openai.AppChatGPT.Unsafe(question, mod) {
		//	replay = dirtywords.DirtyWords().Replace(replay)
		//}
		res.Echo(ctx)
	} else {
		openai.AppChatGPT.ConvChatStream(ctx.Writer, question, mod, &openai.User{UID: uid}, stop)
	}

}

func ConversationHandle(ctx *gin.Context) {
	question := ctx.Query("q")
	mod := ctx.Query("smodel")

	if !openai.AppChatGPT.CheckQuestion(question) && !openai.AppChatGPT.Unsafe(question, mod) {
		helper.Echo(ctx, int(code.SUCCESS), "抱歉没有找到与“"+question+"”相关的信息")
		return
	}

	res := openai.AppChatGPT.ConvConversation(question, mod, nil)
	//if mcode == code.SUCCESS && !openai.AppChatGPT.Unsafe(question, mod) {
	//	replay = dirtywords.DirtyWords().Replace(replay)
	//}
	res.Echo(ctx)
}
